package com.assignwork.service.impl;

import com.assignwork.dto.CorpCargoSearchDTO;
import com.assignwork.dto.ShipAgentSearchDTO;
import com.assignwork.dto.ShipAgentSubmitDTO;
import com.assignwork.entity.ShipAgent;
import com.assignwork.mapper.ShipAgentMapper;
import com.assignwork.service.IShipAgentService;
import com.assignwork.util.PinyinUtil;
import com.assignwork.vo.CorpCargoListVO;
import com.assignwork.vo.ShipAgentListVO;
import com.assignwork.vo.ShipAgentPageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ShipAgentServiceImpl
 * @Description 船代接口实现
 * @Author dujianyu
 * @Date 2023/8/22 9:10
 */
@Service
public class ShipAgentServiceImpl extends BaseServiceImpl<ShipAgentMapper, ShipAgent> implements IShipAgentService {
    @Override
    public IPage<ShipAgentPageVO> selectShipAgentPage(ShipAgentSearchDTO dto, IPage<ShipAgentPageVO> page) {
        List<ShipAgentPageVO> data = baseMapper.selectShipAgentPage(dto, page);
        return page.setRecords(data);
    }

    @Override
    public List<ShipAgentListVO> selectShipAgentList(ShipAgentSearchDTO dto) {
        List<ShipAgentListVO> data = baseMapper.selectShipAgentList(dto);
        for (ShipAgentListVO vo : data) {
            vo.setShipAgentSearchName(vo.getShipAgentName() + "(" + vo.getPinyinHead() + ")");
        }
        return data;
    }

    @Override
    public boolean pinyinSet() {
        QueryWrapper<ShipAgent> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ShipAgent::getIsDeleted, 0);
        List<ShipAgent> shipAgents = baseMapper.selectList(queryWrapper);
        for (ShipAgent shipAgent : shipAgents) {
            shipAgent.setPinyinHead(PinyinUtil.getPinYinHeaderChar(shipAgent.getShipAgentName()));
        }
        return super.saveOrUpdateBatch(shipAgents);
    }

    @Override
    public Boolean submitShipAgent(ShipAgentSubmitDTO dto) {
        dto.setPinyinHead(PinyinUtil.getPinYinHeaderChar(dto.getShipAgentName()));
        ShipAgent entity = BeanUtil.copy(dto, ShipAgent.class);
        return super.saveOrUpdate(entity);
    }

    @Override
    public Map<String, String> shipAgentMap() {
        ShipAgentSearchDTO dto = new ShipAgentSearchDTO();
        List<ShipAgentListVO> data = selectShipAgentList(dto);
        return data.stream().collect(Collectors.toMap(ShipAgentListVO::getShipAgentCode, ShipAgentListVO::getShipAgentName, (key2, key1) -> key1));
    }
}
